package com.boang.service;

import com.boang.entity.Student;
import com.boang.util.DbUtil;
import com.boang.util.StringToLocalDateTime;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/*
  @author: 13813
  @date: 2024/12/11 14:04
*/
public class StuService {
    public List<Student> list() throws SQLException {
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("select * from student");
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Student> list = new ArrayList<>();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            String sex = resultSet.getString("sex");
            String birthday = resultSet.getString("birthday");

            //String 转 LocalDateTime
            LocalDateTime birthdayLdt = StringToLocalDateTime.parse(birthday);

            BigDecimal fee = resultSet.getBigDecimal("fee");
            String grad = resultSet.getString("grad");
            int score = resultSet.getInt("score");

            String gmtCreate = resultSet.getString("gmt_create");
            LocalDateTime gmtCreateLdt = StringToLocalDateTime.parse(gmtCreate);

            String gmtModified = resultSet.getString("gmt_modified");
            LocalDateTime gmtModifiedLdt = StringToLocalDateTime.parse(gmtModified);
            Student student = new Student(
                    id,
                    name,
                    age,
                    sex,
                    birthdayLdt,
                    fee,
                    grad,
                    score,
                    gmtCreateLdt,
                    gmtModifiedLdt);
            list.add(student);
        }
        return list;
    }
}
